﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelMergeTool
{
    public partial class Form1 : Form
    {
        #region 初始化
        private string lbText;
        private int rowHeaderIndex;
        private int columnHeaderIndex;
        private int dataStartRowIndex;
        private int dataStartColumnIndex;
        private int dataEndRowIndex;
        private int dataEndColumnIndex;
        private List<string> files;

        public Form1()
        {
            InitializeComponent();
            this.columnHeaderIndex = 2;
            this.rowHeaderIndex = 1;
            this.dataStartRowIndex = 3;
            this.dataStartColumnIndex = 4;
            this.dataEndColumnIndex = 21;
            this.dataEndRowIndex = 27;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.lbText = "点击选择目录按钮，选择需要合并的文件所在文件夹，点击确定按钮即可自动合并并生成合并文件，合并文件所在路径跟所选目录同级";
            this.SetLable();
            this.tbxStartRowIndex.Text = this.dataStartRowIndex.ToString();
            this.tbxStartColumnIndex.Text = this.dataStartColumnIndex.ToString();
            this.tbxEndColumnIndex.Text = this.dataEndColumnIndex.ToString();
            this.tbxEndRowIndex.Text = this.dataEndRowIndex.ToString();
            this.tbxColumnHeaderIndex.Text = this.columnHeaderIndex.ToString();
            this.tbxRowHeaderIndex.Text = this.rowHeaderIndex.ToString();
        }

        private void SetLable()
        {
            this.tbxDesc.Text = this.lbText;
        }
        #endregion


        #region 选择文件
        private void btnFile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "请选择文件路径";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedPath = folderBrowserDialog.SelectedPath;
                DirectoryInfo directoryInfo = new DirectoryInfo(selectedPath);
                this.files = (from s in directoryInfo.GetFiles()
                              where s.Extension.ToLower() == ".xls" || s.Extension.ToLower() == ".xlsx"
                              select s.FullName).ToList<string>();
                this.tbxFile.Text = selectedPath;
            }
        }
        #endregion


        #region 合并
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.files == null || this.files.Count == 0)
            {
                MessageBox.Show("请先选择文件");
            }
            this.lbText = "正在为你处理数据，请稍后。。。";
            this.SetLable();
            try
            {
                this.dataStartRowIndex = int.Parse(this.tbxStartRowIndex.Text) - 1;
                this.dataStartColumnIndex = int.Parse(this.tbxStartColumnIndex.Text) - 1;
                this.columnHeaderIndex = int.Parse(this.tbxColumnHeaderIndex.Text) - 1;
                this.rowHeaderIndex = int.Parse(this.tbxRowHeaderIndex.Text) - 1;
                this.dataEndRowIndex = int.Parse(this.tbxEndRowIndex.Text) - 1;
                this.dataEndColumnIndex = int.Parse(this.tbxEndColumnIndex.Text) - 1;

                int index = 0;
                List<ExcelData> list = new List<ExcelData>();
                this.lbText = string.Format("共 {0} 个文件", this.files.Count);

                this.files.ForEach(delegate (string file)
                {
                    this.lbText = string.Format("{0}\r\n\r\n正在读取第 {1} 个文件：{2}", this.lbText, ++index, file);
                    this.SetLable();
                    var fileData = MergerService.GetFileData(file, this.rowHeaderIndex, this.columnHeaderIndex,
                        this.dataStartRowIndex, this.dataStartColumnIndex, this.dataEndRowIndex, this.dataEndColumnIndex);
                    if (fileData != null && fileData.Count > 0)
                    {
                        list.AddRange(fileData);
                    }
                });
                this.lbText = string.Format("{0}\r\n\r\n数据读取完毕，正在为你合并数据，请耐心等待。。。", this.lbText);
                this.SetLable();
                string savePath;
                MergerService.SaveSumData(this.files[0], list, this.rowHeaderIndex, this.columnHeaderIndex,
                        this.dataStartRowIndex, this.dataStartColumnIndex, this.dataEndRowIndex, this.dataEndColumnIndex, out savePath);

                string msg = string.Format("恭喜你，处理完成! 文件保存路径：{0}", savePath);
                this.lbText = string.Format("{0}\r\n\r\n{1}", this.lbText, msg);
                this.SetLable();
                MessageBox.Show(msg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

    }
}
